<?php
class MantaWindow extends GtkWindow {
	protected $demos = array();
	protected $page_content;
	protected $highlighting = true;
	protected $navtree;
	protected $navtree_model;
	protected $hbox;

	function __construct() {
		parent::__construct();

		$this->connect_simple("destroy", "quit");

		/*if (OS_WIN32)
			$this->set_icon_from_file(p("graphics/manta.ico"));
		else*/
			$this->set_icon_from_file(p("graphics/manta-logo.png"));
		$this->set_title("Manta");
		$this->set_position(Gtk::WIN_POS_CENTER);
		$this->set_default_size(800, 600);

		$this->hbox = new GtkHBox(false, 0);
		$this->add($this->hbox);

		$this->navtree_model = new GtkListStore(GObject::TYPE_PHP_VALUE, GObject::TYPE_STRING, GObject::TYPE_STRING, GObject::TYPE_OBJECT);

		$this->navtree_model->append(array(new MantaHomePage(), null, "Home", GdkPixbuf::new_from_file(p("graphics/home.png"))));
		$this->navtree_model->append(array(new MantaConfigPage(), null, "Configuration", GdkPixbuf::new_from_file(p("graphics/preferences.png"))));
		global $package_manager;
		$this->navtree_model->append(array(&$package_manager, null, "Package Manager", GdkPixbuf::new_from_xpm_data(array(
			"18 18 9 1 0 0",
			" 	s none	m none	c none",
			".	m black	c #880000",
			"X	s iconColor3	m black	c #FF0000",
			"o	s iconColor1	m black	c #000000",
			"O	s iconColor2	m none	c #FFFFFF",
			"+	s iconGray2	m none	c #bdbdbd",
			"@	s iconGray5	m black	c #737373",
			"#	s iconGray4	m none	c #949494",
			"$	s bottomShadowColor	m black	c #5D6069",
			"       ....       ",
			"       .XX.       ",
			"       .XX.       ",
			"       .XX.       ",
			"     ...XX...     ",
			"      .XXXX.      ",
			"       .XX.       ",
			"        ..        ",
			"   ooooooooooooo  ",
			"  oOOOOOOOOOO++@o ",
			" oOOOOOOOOOOOO#@o ",
			" oO+++++++++XX#@o ",
			" oO+++++++++++#@o ",
			" oOoooooooooo+#@o$",
			" oOOOOOOOOOOO+#@o$",
			" o+###########@o$ ",
			"  ooooooooooooo$  ",
			"    $$$$$$$$$$$   "))));
		//TODO: make up something for the package manager other than this XPM data
		foreach ($package_manager->packages as $id=>$package) {
			try {
				$pixbuf = GdkPixbuf::new_from_file_at_size("$package->directory/$id.png", 48, 48);
			} catch (Exception $e) {
				$pixbuf = new GdkPixbuf(Gdk::COLORSPACE_RGB, true, 8, 48, 48);
			}
			$this->navtree_model->append(array(null, $id, $package->name_display, $pixbuf));
		}
		$this->navtree_model->append(array(new MantaAboutPage(), null, "About", GdkPixbuf::new_from_file(p("graphics/about.png"))));

		$this->navtree = new GtkTreeView($this->navtree_model);
		$this->navtree->set_headers_visible(false);

		$this->navtree->append_column(new GtkTreeViewColumn("", new GtkCellRendererPixbuf(), "pixbuf", 3));
		$this->navtree->append_column(new GtkTreeViewColumn("", new GtkCellRendererText(), "text", 2));

		$selection = $this->navtree->get_selection();
		$selection->set_mode(Gtk::SELECTION_SINGLE);
		$selection->connect("changed", array($this, "change_page"));

		$navtreeframe = new GtkScrolledWindow();
		$navtreeframe->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); // Scroll vertically if necessary, but never horizontally
		//$navtreeframe->set_shadow_type(Gtk::SHADOW_IN);
		$navtreeframe->add_with_viewport($this->navtree);
		$this->hbox->pack_start($navtreeframe, false, false);

		$this->show_all();
	}

	function change_page($selection) {
		list($model, $iter) = $selection->get_selected();
		if (!$iter)
			return;
		dprint("Changing to ".$model->get_value($iter, 2)." page");

		$obj = $model->get_value($iter, 0);
		if (!$obj) {
			global $package_manager;
			$obj = $package_manager->packages[$model->get_value($iter, 1)];
		}

		if ($this->page_content)
			$this->hbox->remove($this->page_content);

		$this->page_content = new GtkFrame();
		$this->page_content->set_shadow_type(Gtk::SHADOW_NONE);
		$this->page_content->set_border_width(5);
		$this->hbox->pack_start($this->page_content, true);

		// TODO: see note about create_and_fetch_frame in core.php
		$this->page_content->add($obj->create_frame());
		$this->page_content->show_all();
	}
}
